package xyz.myachin.downloader.workmanagers

import android.content.Context
import androidx.work.Constraints
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import xyz.myachin.downloader.dm.Downloader
import xyz.myachin.downloader.settings.Settings
import java.util.concurrent.TimeUnit


object RemoveDownloadsWorker {
    private lateinit var mWm: WorkManager

    fun init(context: Context) {
        mWm = WorkManager.getInstance(context)
    }

    fun cleanAll(force: Boolean = false) {
        Downloader.getFinishedDownloads()?.let {
            if (it.count == 0) return
        }

        val wReq = OneTimeWorkRequest.Builder(RemoveFilesWork::class.java)

        if (!force) {
            val params = Constraints.Builder()
                .setRequiresBatteryNotLow(Settings.isNeedBattery())
                .setRequiresCharging(Settings.isNeedAcClean())
                .build()
            wReq
                .setConstraints(params)
                .setInitialDelay(Settings.delayClean(), TimeUnit.MINUTES)
        }

        mWm.enqueue(wReq.build())
    }

}